export const tooltipOption = {
  placement: 'top-start',
  popperClass: 'basic-tooltip'
} as const

/**
 * table 处理空值， slot 未使用时生效
 * @param _row
 * @param _column
 * @param cellValue
 * @returns
 */
export const emptyFormatter = (_row: any, _column: any, cellValue: any) =>
  emptyText(cellValue)

/**table时间格式化 */
export const emptyFormatterUnix = (_row: any, _column: any, cellValue: any) => {
  if (!isNaN(+cellValue) && typeof +cellValue === 'number') {
    return formatUnix(+cellValue)
  }
  return emptyText(cellValue)
}

/**
 * 处理空值
 * @param value 原数据
 * @returns
 */
export const emptyText = (value: any) => {
  if (
    value === '' ||
    value === void 0 ||
    value === null ||
    (Array.isArray(value) && !value.length)
  ) {
    return '-'
  }
  return value
}

export function _userMailEll(value: string) {
  const getStr = (str: string, start: number) =>
    `${str.substring(0, start)}***${str.substring(
      str.length - start,
      str.length
    )}`

  if (value.length > 6) {
    return getStr(value, 3)
  }
  if (value.length > 4) {
    return getStr(value, 2)
  }
  if (value.length > 2) {
    return getStr(value, 1)
  }
  if (value.length < 2 && value.length > 0) {
    return `${value.substring(0, 1)}***`
  }
  return '-'
}

/**用户名/邮箱加 ‘*’ */
export function userMailEll(value: string) {
  if (!value) return '-'
  // 是否是邮箱
  const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
  let prefix = ''
  let res = ''
  const isEmail = reg.test(value)
  if (isEmail) {
    const split = value.split('@')
    prefix = split[0]
    const suffix = split[1]
    res = _userMailEll(prefix) + '@' + suffix
  } else {
    res = _userMailEll(value)
  }
  return res
}
